using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

[CustomEditor(typeof(lwAstarGraph))]
public class lwAstarGraphInspector : Editor
{
	int m_nX = 40;
	int m_nY = 40;
	//Vector3 m_vX = new Vector3(59.0f, -3.3f, 0.0f);
	Vector3 m_vX = new Vector3(29.5f, -1.65f, 0.0f);
	Vector3 m_vY = new Vector3(9.3f, 21.0f, 0.0f);

	public override void OnInspectorGUI()
	{
		EditorGUIUtility.LookLikeInspector();


		lwAstarGraph graph = target as lwAstarGraph;

		if (graph.GetNodeCount() == 0)
		{
			EditorGUILayout.BeginVertical();
				EditorGUILayout.BeginHorizontal();
					EditorGUILayout.LabelField("Width");
					m_nX = EditorGUILayout.IntField(m_nX);
					EditorGUILayout.LabelField("Height");
					m_nY = EditorGUILayout.IntField(m_nY);
				EditorGUILayout.EndHorizontal();

				m_vX = EditorGUILayout.Vector3Field("X vector", m_vX);
				m_vY = EditorGUILayout.Vector3Field("Y vector", m_vY);

				EditorGUILayout.BeginHorizontal();
					if (GUILayout.Button("Generate grid graph"))
					{
						graph.GenerateGrid(m_nX, m_nY, m_vX, m_vY);
						EditorUtility.SetDirty(graph.gameObject);
					}
				EditorGUILayout.EndHorizontal();

				EditorGUILayout.BeginHorizontal();
					if (GUILayout.Button("Detect Existing Nodes"))
					{
						graph.RebuildNodeList();
						graph.UpdateLinks();
						EditorUtility.SetDirty(graph.gameObject);
					}
				EditorGUILayout.EndHorizontal();

				EditorGUILayout.BeginHorizontal();
					if (GUILayout.Button("Add a first node"))
					{
						GameObject go = new GameObject("PathNode");
						lwAstarNode node = go.AddComponent<lwAstarNode>();
						go.transform.parent = (target as lwAstarGraph).transform;
						go.transform.localPosition = new Vector3(0,0,0);
						node.m_Links = new lwAstarNode.lwAstarLink[0];

						graph.RebuildNodeList();
						graph.UpdateLinks();
						EditorUtility.SetDirty(graph.gameObject);
					}
				EditorGUILayout.EndHorizontal();
				
			EditorGUILayout.EndVertical();
		}
		else
		{
			if (GUILayout.Button("Wipe all nodes"))
			{
				lwAstarNode current = null;
				while(graph.GetNodeCount()>0)
				{
					current = graph.GetNode(0);
					if (current.gameObject != null)
					{
						//graph.UnlinkNode(current);
						Object.DestroyImmediate(current.gameObject);
					}
				}
			}
			
			if (GUILayout.Button("Break prefab link for easy editing"))
			{
				PrefabUtility.DisconnectPrefabInstance(graph);
			}
				
			 
		}
	}
		
}
